package com.valid;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.executable.ExecutableValidator;

import org.hibernate.validator.HibernateValidator;

public abstract class ValidatorUtil {

    public static Validator validator() {
        return Validation.buildDefaultValidatorFactory().getValidator();
    }

    public static Validator failFastValidator(){
        return Validation.byProvider(HibernateValidator.class)
                .configure().failFast(true)
                .buildValidatorFactory().getValidator();
    }
    public static  Set valid(UserInfo userInfo){
        Set<ConstraintViolation<UserInfo>> validate = validator().validate(userInfo);
        return validate;
    }

    public static ExecutableValidator obtainExecutableValidator() {
        return validator().forExecutables();
    }

    public static <T> void printViolations(Set<ConstraintViolation<T>> violations) {
        violations.stream().map(v -> v.getPropertyPath() + " " + v.getMessage() + ": " + v.getInvalidValue()).forEach(System.out::println);
    }

}